package cn.jingdalei.org.controller;

import cn.jingdalei.basic.exception.BusinessException;
import cn.jingdalei.basic.util.AjaxResult;
import cn.jingdalei.basic.util.PageList;
import cn.jingdalei.org.domain.Department;
import cn.jingdalei.org.domain.Shop;
import cn.jingdalei.org.query.ShopQuery;
import cn.jingdalei.org.service.IDepartmentService;
import cn.jingdalei.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

// @Controller
@RestController  // (controller + responseBoby[返回json格式] )
@RequestMapping("/shop")
@Api(tags = "店铺接口",description = "店铺接口详细描述")
public class ShopController {

    @Autowired
    private IShopService shopService;

    @Autowired
    private IDepartmentService departmentService;

    /**
     * 添加或者修改
     * @param shop
     * @return
     */
    @PutMapping
    @ApiOperation(value = "店铺添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult addOrUpdate(@RequestBody  Shop shop){
        try {
            if (shop.getId()==null){
                shopService.add(shop);
            }else {
                shopService.update(shop);
            }
            return AjaxResult.me();
        } catch (Exception e){
            e.printStackTrace();
            // 避免多次创建对象
            return  AjaxResult.me().setMessage("操作失败!!");
        }
    }
    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除",notes = "如果有id就是修改否则就是添加")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            // 链式编程思想
            return AjaxResult.me().setMessage("删除失败!!");
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try {
            shopService.patchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            // 链式编程思想
            return AjaxResult.me().setMessage("删除失败!!");
        }
    }

    /**
     * 查询单个
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询",notes = "如果有id就是修改否则就是添加")
    public Shop findById(@PathVariable("id") Long id){
        return shopService.queryById(id);
    }

    /**
     *  查询所有
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询全部店铺信息",notes = "如果有id就是修改否则就是添加")
    public List<Shop> findAll(){
        return shopService.queryAll();
    }

    /**
     * 分页查询
     * axios get不支持传对象
     * @param query
     * @return
     */
    @PostMapping         // @GetMapping()不能传对象
    @ApiOperation(value = "分页查询店铺信息",notes = "如果有id就是修改否则就是添加")
    public PageList<Shop> findPage(@RequestBody ShopQuery query){
        return shopService.queryData(query);
    }

    /**
     * 店铺注册
     * @return
     */
    @PostMapping("/settlement")
    public AjaxResult setTlement(@RequestBody Shop shop){
        try {
            shopService.setTlement(shop);
            return AjaxResult.me();
        } catch (BusinessException e) { // 业务异常捕捉
            e.printStackTrace();
            return AjaxResult.me().setMessage("入驻失败"+e.getMessage());
        } catch (Exception e) { // 系统错误 500
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统错误"+e.getMessage());
        }
    }

}
